#R-Script to replicate paper:
#Preference Representation and the Influence of Political Parties in Majoritarian vs. 
#Proportional Systems: An Empirical Test
#British Journal of Political Science
#Authors: DAVID STADELMANN, MARCO PORTMANN AND REINER EICHENBERGER
#Please address questions to Marco Portmann (marco.portmann@unifr.ch) or David Stadelmann (david.stadelmann@uni-bayreuth.de)


#! Packets + functions 
#Packets
library(car)       
library(lmtest)
library(sandwich)
library(MASS)
library(rms)
library(alr3)

#set directory
setwd(" - set directory where files are located - ")

FunctionsDir <- ' - set directory where files are located - '
source(paste(FunctionsDir, 'DiscreteEffects.r', sep = ''), echo=F, max.deparse.length=150) #skript to calculate discrete effects
source(paste(FunctionsDir, 'PrintModels.r', sep = ''), echo=F, max.deparse.length=150) #skript to design tables

#small function to prepare DE conveniently for later printing
dxPrint <- function(objLRM){
  tempDECS1    = DiscreteEffectDiD(objLRM, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1)) #Pure interaction effect Left
  tempDECS2    = DiscreteEffectDiD(objLRM, "PartyRight", "IsCS", medianspecial=c("IsCS", 1)) #Pure interaction effect Right
  tempDECSFull = DiscreteEffects(objLRM, medianspecial=c("IsCS", 1)) #Full effect of left and right in CS
  
  dx = rbind(
    DiscreteEffects(objLRM, medianspecial=c("IsCS", 0)),
    "DEIsCS * PartyLeft" =tempDECS1["Discrete Change",],
    "DEIsCS * PartyRight" =tempDECS2["Discrete Change",],
    "DEIsCSFull * PartyLeft" =tempDECSFull["DEPartyLeft",],
    "DEIsCSFull * PartyRight" =tempDECSFull["DEPartyRight",] 
  ) 
  return(dx)
}






#!Load data
mydata <- read.table("electoralsystems_data.txt", header= TRUE, sep = "\t", quote="\"", dec=".", na.strings = "NA")

# NA & Abstentions (additional variables)   
mydata$AverageAbstainHigh <-0 
HighAbstainNC <- quantile(subset(mydata, NationalCouncil == 1)$mpAverageAbstain, 0.9)
HighAbstainCS <- quantile(subset(mydata, NationalCouncil == 0)$mpAverageAbstain, 0.9)

mydata[mydata$NationalCouncil == 1 & mydata$mpAverageAbstain > HighAbstainNC, 'AverageAbstainHigh'] <- 1  
mydata[mydata$NationalCouncil == 0 & mydata$mpAverageAbstain > HighAbstainCS, 'AverageAbstainHigh'] <- 1  

HighNACS <- quantile(mydata$mpAverageNA, 0.9, na.rm = T)
mydata[is.na(mydata$mpAverageNA), 'mpAverageNA'] <- 0
mydata$AverageNAHigh <- (mydata$mpAverageNA > HighNACS) *1

#datadist definition
dd <- datadist(data.frame(mydata))
options(datadist="dd")

#create datasets for National Council (nc) and Council of States (cs)
mync <- subset(mydata, NationalCouncil == 1)
mycs <- subset(mydata, NationalCouncil == 0)






#! Results
#!2 Infos on dataset
#Refs and Nb. of refs
sort(unique(mydata$ref_id))
length(unique(mydata$ref_id)) #48

#Nb. of politicians in CS
length(unique(mycs$mp_id)) #84

#Nb. of politicians in NC
length(unique(mync$mp_id)) #352

#center party affiliation is on average more common in the Council of States than in the National Council
CenterFreguencyNC <- length(unique(subset(mync, PartyCenter==1)$mp_id))/length(unique(mync$mp_id)) #0.379562
CenterFreguencyCS <-length(unique(subset(mycs, PartyCenter==1)$mp_id))/length(unique(mycs$mp_id)) #0.6323529
CenterFreguencyCS/CenterFreguencyNC #1.52381



#!2 Table 1 (+ info in text)
NCLeft <- subset(mync, PartyLeft==1)$CantonMatch
NCCenter <- subset(mync, PartyCenter==1)$CantonMatch
NCRight <- subset(mync, PartyRight==1)$CantonMatch

CSLeft <- subset(mycs, PartyLeft==1)$CantonMatch
CSCenter <- subset(mycs, PartyCenter==1)$CantonMatch
CSRight <- subset(mycs, PartyRight==1)$CantonMatch

DiffInDiff <- ols(CantonMatch ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight     
         ,
         data=subset(mydata), tol=1E-10,  x=T, y=T)
DiffInDiff <- robcov(DiffInDiff, cluster=subset(mydata)$mp_id)

write.table(
   matrix(c(
      round(t.test(NCLeft, mu = 0.5 )$estimate, digits=3)     ,
      round(t.test(NCCenter, mu = 0.5 )$estimate, digits=3) ,
      round(t.test(NCRight, mu = 0.5 )$estimate, digits=3)   ,
      formatnumber(c(mean(NCCenter) - mean(NCLeft)), as.numeric(t.test(NCCenter, NCLeft)$statistic), decimals=3),
      formatnumber(c(mean(NCCenter) - mean(NCRight)), t.test(NCCenter, NCRight)$statistic, decimals=3),
      round(t.test(mync$CantonMatch, mu = 0.5 )$estimate, digits=3) ,
      round(t.test(CSLeft, mu = 0.5 )$estimate, digits=3)     ,
      round(t.test(CSCenter, mu = 0.5 )$estimate, digits=3) ,
      round(t.test(CSRight, mu = 0.5 )$estimate, digits=3) ,
      formatnumber(c(mean(CSCenter) - mean(CSLeft)), t.test(CSCenter, CSLeft)$statistic, decimals=3),
      formatnumber(c(mean(CSCenter) - mean(CSRight)), t.test(CSCenter, CSRight)$statistic, decimals=3),
      round(t.test(mycs$CantonMatch, mu = 0.5 )$estimate, digits=3),
      formatnumber(c(mean(CSLeft) - mean(NCLeft)), t.test(CSLeft, NCLeft)$statistic, decimals=3),
      formatnumber(c(mean(CSCenter) - mean(NCCenter)), t.test(CSCenter, NCCenter)$statistic, decimals=3),      
      formatnumber(c(mean(CSRight) - mean(NCRight)), t.test(CSRight, NCRight)$statistic, decimals=3), 
      formatnumber(DiffInDiff$coefficients[5], c(DiffInDiff$coefficients[5]/sqrt(diag(DiffInDiff$var))[5]), decimals=3), 
      formatnumber(DiffInDiff$coefficients[6], c(DiffInDiff$coefficients[6]/sqrt(diag(DiffInDiff$var))[6]), decimals=3), 
      formatnumber(c(mean(mycs$CantonMatch) - mean(mync$CantonMatch)), t.test(mycs$CantonMatch, mync$CantonMatch)$statistic, decimals=3)        
      ), 
      ncol=6, byrow=T)
  ,file="out/tab01.csv", sep=";", col.names = NA , row.names = TRUE)

#There is no significant difference in congruence levels between proportionally elected politicians from left and right parties. 
t.test(NCRight, NCLeft)



#!2 Table 2
#!3 2 Sets: Left-Right
f21 <- lrm(CantonMatch ~ 
         + PartyRight + PartyLeft
         + ref_type
         ,
         data=subset(mync), tol=1E-10,  x=T, y=T)
c21 <- robcov(f21, cluster=subset(mync)$mp_id)
c21
d21 <- DiscreteEffects(c21)
d21
contrast(c21, list(PartyLeft=1), list(PartyRight=1)) 

f22 <- lrm(CantonMatch ~

         + PartyRight + PartyLeft
         + ref_type
         ,
         data=subset(mycs), tol=1E-10,  x=T, y=T)
c22 <- robcov(f22, cluster=subset(mycs)$mp_id)
c22
d22 <- DiscreteEffects(c22)
d22
contrast(c22, list(PartyLeft=1), list(PartyRight=1)) 

             
#!3 Interaktion model (Baseline)
f23 <- lrm(CantonMatch ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type        
         ,
         data=subset(mydata), tol=1E-10,  x=T, y=T)
c23 <- robcov(f23, cluster=subset(mydata)$mp_id)
c23
d23 <- DiscreteEffects(c23, medianspecial=c("IsCS", 0))
d23
d23CS1 <- DiscreteEffects(c23, medianspecial=c("IsCS", 1))
d23CS1
contrast(c23, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c23, list(PartyRight=1, IsCS=1), list(IsCS=1))   

contrast(c23, list(PartyLeft=1, IsCS=0), list(PartyRight=1, IsCS=0)) 
contrast(c23, list(PartyLeft=1, IsCS=1), list(PartyRight=1, IsCS=1)) 


# from left to right in CS (for text)
d23CS1 <- DiscreteEffects(c23, medianspecial=c("IsCS", 1))
d23CS1

# from left NC to left CS (for text)
d23N2 <- DiscreteEffects(c23, medianspecial=c("PartyLeft", 1))
d23N2

# from right NC to right CS (for text)
d23N3 <- DiscreteEffects(c23, medianspecial=c("PartyRight", 1))
d23N3


#!3 Interaktion: OLS
f24 <- ols(CantonMatch ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type        
         ,
         data=subset(mydata), tol=1E-10,  x=T, y=T)
c24 <- robcov(f24, cluster=subset(mydata)$mp_id)
c24



#!3 Export
regressors <- c("IsCS",
                "PartyLeft", "PartyRight",
                "IsCS * PartyLeft",
                "IsCS * PartyRight",               
                "IsCSFull * PartyLeft",
                "IsCSFull * PartyRight"
                )
                            
output <-
mmp(              regressors=regressors,      #  Ohne regressors sollte vollst�ndige Liste
                                               #  mit allen Variablen ausgegeben werden
	                file="clipboard",                                     # ... funktioniert NUR �ber clipboard
                  decimals=3,
                  factors=c("ref_type"),
                  bottom.matter=c("r.squared", "Brier", "N"), # nicht notwendiges Argument
                  allmodels=list(c21, c22, c23, c24, dxPrint(c23)),
                  models.names=c("col 1", "col 2", "col 3", "col 4", "Discrete effects for text and table"), # nicht notwendiges Argument
                  )
write.table(output, file="out/tab02.csv", sep=";")



#!2 Figure 2 
altc23 <- update(c23, . ~ . - PartyLeft - IsCS*PartyLeft + PartyCenter + PartyCenter*IsCS)
altd23 <- DiscreteEffects(altc23, medianspecial=c("IsCS", 0))
altd23CS1 <- DiscreteEffects(altc23, medianspecial=c("IsCS", 1))


#panel (a) - takes matrix to clipboard => copy into excel file (yellow space)
#contact authors for excel file to create figures more quickly
write.table(
  matrix(c(
    round(d23["DEPartyLeft","Estimate"], digits=5)      ,       #NC: Center => Left
    round(d23CS1["DEPartyLeft","Estimate"], digits=5)     ,     #NC: Center => Left
    round(d23["DEPartyRight","Estimate"], digits=5)     ,       #NC: Center => Right
    round(d23CS1["DEPartyRight","Estimate"], digits=5)     ,    #NC: Center => Right    
    round(altd23["DEPartyRight","Estimate"], digits=5)     ,    #NC: Left => Right
    round(altd23CS1["DEPartyRight","Estimate"], digits=5),      #NC: Left => Right    

    round(d23["DEPartyLeft","SE"], digits=5)      ,       #NC: Center => Left
    round(d23CS1["DEPartyLeft","SE"], digits=5)     ,     #NC: Center => Left
    round(d23["DEPartyRight","SE"], digits=5)     ,       #NC: Center => Right
    round(d23CS1["DEPartyRight","SE"], digits=5)     ,    #NC: Center => Right    
    round(altd23["DEPartyRight","SE"], digits=5)     ,    #NC: Left => Right
    round(altd23CS1["DEPartyRight","SE"], digits=5)       #NC: Left => Right    
    
    ), 
         ncol=2, byrow=T)
  ,"clipboard", sep="\t", col.names = NA , row.names = TRUE)    
 

#panel (b)  - takes matrix to clipboard => copy into excel file (yellow space)
#contact authors for excel file to create figures more quickly
write.table(
  matrix(c(
    round(d23N2["DEIsCS","Estimate"], digits=5)     ,        #Left: C => CS
    round(d23N2["DEIsCS","SE"], digits=5)     ,        #Left: NC => CS
    round(d23  ["DEIsCS","Estimate"], digits=5)      ,       #Center: NC => CS
    round(d23  ["DEIsCS","SE"], digits=5)      ,       #Center: NC => CS
    round(d23N3["DEIsCS","Estimate"], digits=5)     ,        #Right: NC => CS
    round(d23N3["DEIsCS","SE"], digits=5)              #Right: NC => CS
    
  ), 
         ncol=2, byrow=T)
  ,"clipboard", sep="\t", col.names = NA , row.names = TRUE)   



#!2 Table 3 (Robustness 1)
#!3 controlling for NA + Abstain
f31 <- lrm(CantonMatch ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type
         + mpAverageNA
         + mpAverageAbstain
         ,
         data=subset(mydata), tol=1E-10,  x=T, y=T)
c31 <- robcov(f31, cluster=subset(mydata)$mp_id)
c31
DiscreteEffects(c31, medianspecial=c("IsCS", 0))
DiscreteEffects(c31, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c31, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c31, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c31, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c31, list(PartyRight=1, IsCS=1), list(IsCS=1))


#!3 excluding NA         
f32 <- lrm(CantonMatch ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type
         + mpAverageAbstain
#         + mpAverageNA
         ,
         data=subset(mydata, AverageNAHigh == 0 ), tol=1E-10,  x=T, y=T)
c32 <- robcov(f32, cluster=subset(mydata, AverageNAHigh == 0 )$mp_id)
c32
DiscreteEffects(c32, medianspecial=c("IsCS", 0))
DiscreteEffects(c32, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c32, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c32, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c32, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c32, list(PartyRight=1, IsCS=1), list(IsCS=1))


#!3 alternative party classification 
#for CSP und der MCR
tempdat <- mydata
tempdat$PartyLeft <- tempdat$PartyLeftAlt
tempdat$PartyRight <- tempdat$PartyRightAlt
f33 <- lrm(CantonMatch ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type
         ,
         data=subset(mydata), tol=1E-10,  x=T, y=T)
c33 <- robcov(f33, cluster=subset(mydata)$mp_id)
c33
DiscreteEffects(c33, medianspecial=c("IsCS", 0))
DiscreteEffects(c33, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c33, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c33, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c33, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c33, list(PartyRight=1, IsCS=1), list(IsCS=1))
rm(tempdat)


#!3 only politicians parties CS and NC 
#(restricted sample)
f34 <- lrm(CantonMatch ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type
         ,
         data=subset(mydata, PartyCS == 1), tol=1E-10,  x=T, y=T)
c34 <- robcov(f34, cluster=subset(mydata, PartyCS == 1)$mp_id)
c34
DiscreteEffects(c34, medianspecial=c("IsCS", 0))
DiscreteEffects(c34, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c34, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c34, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c34, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c34, list(PartyRight=1, IsCS=1), list(IsCS=1)) 


#!3 Personal Characteristics
f35 <- lrm(CantonMatch ~
         + IsCS     
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type
         + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice 
,
         data=subset(mydata), tol=1E-10,  x=T, y=T)
c35 <- robcov(f35, cluster=subset(mydata)$mp_id)
c35
DiscreteEffects(c35, medianspecial=c("IsCS", 0))["DEPartyLeft",]
DiscreteEffects(c35, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c35, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c35, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c35, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c35, list(PartyRight=1, IsCS=1), list(IsCS=1))


#!3 Canton FE
f36 <- lrm(CantonMatch ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type         
         + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice 
         + canton   
         ,
         data=subset(mydata), tol=1E-10,  x=T, y=T)
c36 <- robcov(f36, cluster=subset(mydata)$mp_id)
c36
DiscreteEffects(c36, medianspecial=c("IsCS", 0))
DiscreteEffects(c36, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c36, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c36, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c36, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c36, list(PartyRight=1, IsCS=1), list(IsCS=1)) 


#!3 Ref FE 
f37 <- lrm(CantonMatch ~
            + IsCS
           + PartyLeft + PartyRight
           + IsCS*PartyLeft + IsCS*PartyRight
           # + ref_type                      #instead ref_id
           + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice  
           + fref_id 
           ,
           data=subset(mydata), tol=1E-10,  x=T, y=T)
c37 <- robcov(f37, cluster=subset(mydata)$mp_id)
c37
DiscreteEffects(c37, medianspecial=c("IsCS", 0))
DiscreteEffects(c37, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c37, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c37, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c37, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c37, list(PartyRight=1, IsCS=1), list(IsCS=1))   


#!3 Export
regressors <- c("IsCS",
                "PartyLeft", "PartyRight",
                "IsCS * PartyLeft",
                "IsCS * PartyRight", 
                "mpAverageNA", "mpAverageAbstain",
                "Sex", "Age", "HasChildren", "MasterDoktor", "BornOutside", "TimeinOffice",
                "IsCSFull * PartyLeft",
                "IsCSFull * PartyRight"
                )
                
output <-
mmp(              regressors=regressors,      #  Ohne regressors sollte vollst�ndige Liste
                                               #  mit allen Variablen ausgegeben werden
	                file="clipboard",                                     # ... funktioniert NUR �ber clipboard
                  decimals=3,
                  factors = c("ref_type", "canton","fref_id"),
                  
                  bottom.matter=c("r.squared", "Brier", "N"), # nicht notwendiges Argument
                  allmodels=list(dxPrint(c31), dxPrint(c32), dxPrint(c33), dxPrint(c34), dxPrint(c35), dxPrint(c36), dxPrint(c37), c31, c32, c33, c34, c35, c36, c37),
                  models.names=c("Discrete Effect 31", "Discrete Effect 32", "Discrete Effect 33", "Discrete Effect 34", "Discrete Effect 35", "Discrete Effect 36", "Discrete Effect 37", "Logit Coef 31", "Logit Coef 32", "Logit Coef 33", "Logit Coef 34", "Logit Coef 35", "Logit Coef 36", "Logit Coef 37"), # nicht notwendiges Argument
                  )
write.table(output, file="out/tab03.csv", sep=";")


 
#!2 Table 4 (Refinement)
#former table 4 with additional robustness tests has been moved to online appendix
#!3 National Match for PR (no controls) 
f51 <- lrm(MatchNCNation ~
             + IsCS
           + PartyLeft + PartyRight
           + IsCS*PartyLeft + IsCS*PartyRight
           + ref_type  
           ,
           data=subset(mydata), tol=1E-10,  x=T, y=T)
c51 <- robcov(f51, cluster=subset(mydata)$mp_id)
c51
DiscreteEffects(c51, medianspecial=c("IsCS", 0))
DiscreteEffects(c51, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c51, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c51, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c51, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c51, list(PartyRight=1, IsCS=1), list(IsCS=1))  


#!3 National Match for PR (no controls) 
f52 <- lrm(MatchNCNation ~
           + IsCS
           + PartyLeft + PartyRight
           + IsCS*PartyLeft + IsCS*PartyRight
           + ref_type                 
           + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice 
           ,
           data=subset(mydata), tol=1E-10,  x=T, y=T)
c52 <- robcov(f52, cluster=subset(mydata)$mp_id)
c52
DiscreteEffects(c52, medianspecial=c("IsCS", 0))
DiscreteEffects(c52, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c52, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c52, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c52, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c52, list(PartyRight=1, IsCS=1), list(IsCS=1))    


#!3 Seats < 3 
#without controls because not possible in small cantons
f53 <- lrm(CantonMatch ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type
#         + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice 
,
           data=subset(mydata, Seats<3), tol=1E-10,  x=T, y=T)
c53 <- robcov(f53, cluster=subset(mydata, Seats<3)$mp_id)
c53
DiscreteEffects(c53, medianspecial=c("IsCS", 0))
DiscreteEffects(c53, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c53, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c53, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c53, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c53, list(PartyRight=1, IsCS=1), list(IsCS=1))


#!3 Seats >= 3 cantons
#without controls because not possible in small cantons
f54 <- lrm(MatchNCNation ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type
#         + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice 
,
           data=subset(mydata, Seats>=3), tol=1E-10,  x=T, y=T)
c54 <- robcov(f54, cluster=subset(mydata, Seats>=3)$mp_id)
c54
DiscreteEffects(c54, medianspecial=c("IsCS", 0))
DiscreteEffects(c54, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c54, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c54, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c54, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c54, list(PartyRight=1, IsCS=1), list(IsCS=1)) 


#!3 Seats sweet spot
#without controls because not possible in small cantons
f55 <- lrm(MatchNCNation ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type
#         + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice 
,
           data=subset(mydata, Seats>3 & Seats<9), tol=1E-10,  x=T, y=T)
c55 <- robcov(f55, cluster=subset(mydata, Seats>3 & Seats<9)$mp_id)
c55
DiscreteEffects(c55, medianspecial=c("IsCS", 0))
DiscreteEffects(c55, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c55, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c55, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c55, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c55, list(PartyRight=1, IsCS=1), list(IsCS=1))


#!3 PartyMatch (average)
f56 <- lrm(MatchWithParty ~
         + IsCS
         + ref_type
         ,
         data=subset(mydata, NoPartyPosition == 0 ), tol=1E-10,  x=T, y=T)
c56 <- robcov(f56, cluster=subset(mydata,  NoPartyPosition == 0  )$mp_id)
c56
d56 <- DiscreteEffects(c56)
d56


#!3 PartyMatch (Interactions)
f57 <- lrm(MatchWithParty ~
         + IsCS
         + PartyLeft + PartyRight
         + IsCS*PartyLeft + IsCS*PartyRight
         + ref_type
         ,
         data=subset(mydata, NoPartyPosition == 0), tol=1E-10,  x=T, y=T)
c57 <- robcov(f57, cluster=subset(mydata, NoPartyPosition == 0)$mp_id)
c57
DiscreteEffects(c57, medianspecial=c("IsCS", 0))
DiscreteEffects(c57, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c57, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c57, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c57, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c57, list(PartyRight=1, IsCS=1), list(IsCS=1)) 
 

#!3 Export
regressors <- c("IsCS",
                "PartyLeft", "PartyRight",
                "IsCS * PartyLeft",
                "IsCS * PartyRight",
                "IsCSFull * PartyLeft",
                "IsCSFull * PartyRight",
                "Personal characteristics"      
)

output <-
  mmp(              regressors=regressors,      #  Ohne regressors sollte vollst�ndige Liste
                    #  mit allen Variablen ausgegeben werden
                    file="clipboard",                                     # ... funktioniert NUR �ber clipboard
                    decimals=3,
                    factors = c("ref_type"),
                    
                    bottom.matter=c("r.squared", "Brier", "N"), # nicht notwendiges Argument
                    allmodels=list(dxPrint(c51), dxPrint(c52), dxPrint(c53), dxPrint(c54), dxPrint(c55), d56, dxPrint(c57), c51, c52, c53, c54, c55, c56, c57),
                    models.names=c("Discrete Effect 51", "Discrete Effect 52", "Discrete Effect 53", "Discrete Effect 54", "Discrete Effect 55", "Discrete Effect 56", "Discrete Effect 57", "Logit Coef 51", "Logit Coef 52", "Logit Coef 53", "Logit Coef 54", "Logit Coef 55", "Logit Coef 56", "Logit Coef 57"), # nicht notwendiges Argument
  )
write.table(output, file="out/tab04.csv", sep=";")

 

#!2 Table 5
#!3 2 Sets: Major parties
f61 <- lrm(CantonMatch ~
         + PartyFDP + PartyCVP + PartySP 
         + Sex + Age + HasChildren + BornOutside + TimeinOffice 
         + ref_type       
         ,
         data=subset(mync), tol=1E-10,  x=T, y=T)
c61 <- robcov(f61, cluster=subset(mync)$mp_id)
c61
d61 <- DiscreteEffects(c61)
d61
                        
f62 <- lrm(CantonMatch ~

         + PartyFDP + PartyCVP + PartySP 
         + Sex + Age + HasChildren + BornOutside + TimeinOffice  
         + ref_type        
         ,
         data=subset(mycs), tol=1E-10,  x=T, y=T)
c62 <- robcov(f62, cluster=subset(mycs)$ref_id)
c62
d62 <- DiscreteEffects(c62)
d62


#!3 2 Sets: Major parties and Small party Dummy
f63 <- lrm(CantonMatch ~

         + PartyFDP + PartyCVP + PartySP + PartySmallN 
         + Sex + Age + HasChildren + BornOutside + TimeinOffice 
         + ref_type       
         ,
         data=subset(mync), tol=1E-10,  x=T, y=T)
c63 <- robcov(f63, cluster=subset(mync)$mp_id)
c63
d63 <- DiscreteEffects(c63)
d63

                         
f64 <- lrm(CantonMatch ~

         + PartyFDP + PartyCVP + PartySP + PartySmallN 
         + Sex + Age + HasChildren + BornOutside + TimeinOffice  
         + ref_type        
         ,
         data=subset(mycs), tol=1E-10,  x=T, y=T)
c64 <- robcov(f64, cluster=subset(mycs)$ref_id)
c64
d64 <- DiscreteEffects(c64)
d64


#!3 2 Sets: all parties
f65 <- lrm(CantonMatch ~

         + PartyFDP + PartyCVP + PartySP + PartyGLP + PartyBDP + PartyGPS + PartySmallLeft + PartySmallCenter + PartySmallRight  
         + ref_type       
         ,
         data=subset(mync), tol=1E-10,  x=T, y=T)
c65 <- robcov(f65, cluster=subset(mync)$mp_id)
c65
d65 <- DiscreteEffects(c65)
d65

                         
f66 <- lrm(CantonMatch ~

         + PartyFDP + PartyCVP + PartySP + PartyGLP + PartyBDP + PartyGPS +PartySmallRight  
 
         + ref_type        
         ,
         data=subset(mycs), tol=1E-10,  x=T, y=T)
c66 <- robcov(f66, cluster=subset(mycs)$ref_id)
c66
d66 <- DiscreteEffects(c66)
d66


#!3 Export
regressors <- c(         
                "PartyCVP",
                "PartyFDP",
                "PartySP",
                "PartySmallN",
                "PartyGLP",
                "PartyBDP",
                "PartyGPS",
                "PartySmallLeft",
                "PartySmallCenter",
                "PartySmallRight",

                "ctrl Personal"       
                )
                              
output <-
mmp(              regressors=regressors,      #  Ohne regressors sollte vollst�ndige Liste
                                               #  mit allen Variablen ausgegeben werden
	                file="clipboard",                                     # ... funktioniert NUR �ber clipboard
                  decimals=3,
                  bottom.matter=c("r.squared", "Brier", "N"), # nicht notwendiges Argument
                  allmodels=list(d61, d62, d63, d64, d65, d66, c61, c62, c63, c64, c65, c66),
                  factors = c("ref_type", "canton", "ref_id"),
                  models.names=c("Discrete Effect 61", "Discrete Effect 62", "Discrete Effect 63", "Discrete Effect 64", "Discrete Effect 65", "Discrete Effect 66", "Logit Coef 61", "Logit Coef 62", "Logit Coef 63", "Logit Coef 64", "Logit Coef 65", "Logit Coef 66"), # nicht notwendiges Argument
                  )
write.table(output, file="out/tab05.csv", sep=";")



#! Appendix
#!2 Appendix A3 - Descriptive statistics 
#takes matrix to clipboard => copy into excel file (yellow space)
#contact authors for excel file to create figures more quickly
yx <- mydata[,
             c("CantonMatch", 
               "IsCS", 
               
               "PartyLeft", "PartyCenter", "PartyRight",
               "mpAverageNA", "mpAverageAbstain",
             
               "Sex", "Age", "HasChildren", "MasterDoktor", "BornOutside", "TimeinOffice",
               
               "PartyCVP",
               "PartyFDP",
               "PartySP",
               "PartySVP",                
               "PartySmallN", 
               "PartyGLP",
               "PartyBDP", 
               "PartyGPS", 
               "PartySmallLeft",
               "PartySmallCenter",
               "PartySmallRight"               
               
             )
             ]


n <- length(yx)+1
summaryStats <- matrix(0,11,n)
summaryStats[,1] <- c("Desc", "Min","1st Qu.","Median","Mean","3rd Qu.","Max","SD","Cor","Corpvalue", "N")
i <- 2
for (i in 2:n){
    summaryStats[,i] <- c(names(yx)[c(i-1)],summary(yx[,c(i-1)])[1:6],
    sd(yx[,c(i-1)], na.rm=T),
    cor.test(mydata$CantonMatch, yx[,c(i-1)])$estimate,
    cor.test(mydata$CantonMatch, yx[,c(i-1)])$p.value,
    cor.test(mydata$CantonMatch, yx[,c(i-1)])$parameter+2 )
}

write.table(t(summaryStats), file="clipboard", sep="\t" )





#!2 Appendix A3 - further robustness tests
#!3 Nicht die Kantone betreffend
f41 <- lrm(CantonMatch ~
             + IsCS
           + PartyLeft + PartyRight
           + IsCS*PartyLeft + IsCS*PartyRight
           + ref_type               
           + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice,
           data=subset(mydata, Decentralizing!=1 & Centralizing!=1), tol=1E-10,  x=T, y=T)
c41 <- robcov(f41, cluster=subset(mydata, Decentralizing!=1 & Centralizing!=1)$mp_id)
c41
DiscreteEffects(c41, medianspecial=c("IsCS", 0))
DiscreteEffects(c41, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c41, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c41, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c41, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c41, list(PartyRight=1, IsCS=1), list(IsCS=1))   


#!3 Interests
f42 <- lrm(CantonMatch ~
             + IsCS
           + PartyLeft + PartyRight
           + IsCS*PartyLeft + IsCS*PartyRight
           + ref_type                
           + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice,
           data=subset(mydata, Interests>=mean(Interests)), tol=1E-10,  x=T, y=T)
c42 <- robcov(f42, cluster=subset(mydata, Interests>=mean(Interests))$mp_id)
c42
DiscreteEffects(c42, medianspecial=c("IsCS", 0))
DiscreteEffects(c42, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c42, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c42, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c42, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c42, list(PartyRight=1, IsCS=1), list(IsCS=1)) 


#!3 LeftRightAgree
f43 <- lrm(CantonMatch ~
             + IsCS
           + PartyLeft + PartyRight
           + IsCS*PartyLeft + IsCS*PartyRight
           + ref_type              
           + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice,
           ,
           data=subset(mydata, LeftRightAgree==0), tol=1E-10,  x=T, y=T)
c43 <- robcov(f43, cluster=subset(mydata, LeftRightAgree==0)$mp_id)
c43
DiscreteEffects(c43, medianspecial=c("IsCS", 0))
DiscreteEffects(c43, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c43, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c43, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c43, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c43, list(PartyRight=1, IsCS=1), list(IsCS=1))    


#!3 NC narrow margin
f44 <- lrm(CantonMatch ~
           + IsCS
           + PartyLeft + PartyRight
           + IsCS*PartyLeft + IsCS*PartyRight
           + ref_type               
           + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice,
           data=subset(mydata, NCMargin<=mean(NCMargin)), tol=1E-10,  x=T, y=T)
c44 <- robcov(f44, cluster=subset(mydata, NCMargin<=mean(NCMargin))$mp_id)
c44
DiscreteEffects(c44, medianspecial=c("IsCS", 0))
DiscreteEffects(c44, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c44, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c44, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c44, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c44, list(PartyRight=1, IsCS=1), list(IsCS=1))   


#!3 AgreementIndex Weighting
f45 <- lrm(CantonMatch ~
             + IsCS
           + PartyLeft + PartyRight
           + IsCS*PartyLeft + IsCS*PartyRight
           + ref_type                
           + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice, 
           weight=AgreementIndex
           ,
           data=subset(mydata), tol=1E-10,  x=T, y=T)
c45 <- robcov(f45, cluster=subset(mydata)$mp_id)
c45
DiscreteEffects(c45, medianspecial=c("IsCS", 0))
DiscreteEffects(c45, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c45, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c45, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c45, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c45, list(PartyRight=1, IsCS=1), list(IsCS=1))   

  
#!3 Non-close Refs
f46 <- lrm(CantonMatch ~
             + IsCS
           + PartyLeft + PartyRight
           + IsCS*PartyLeft + IsCS*PartyRight
           + ref_type                
           + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice,
           data=subset(mydata, CantonYes > 0.55 | CantonYes < 0.45), tol=1E-10,  x=T, y=T)
c46 <- robcov(f46, cluster=subset(mydata, CantonYes > 0.55 | CantonYes < 0.45)$mp_id)
c46
DiscreteEffects(c46, medianspecial=c("IsCS", 0))
DiscreteEffects(c46, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c46, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c46, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c46, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c46, list(PartyRight=1, IsCS=1), list(IsCS=1))   


#!3 Close Refs Weighting
f47 <- lrm(CantonMatch ~
           + IsCS
           + PartyLeft + PartyRight
           + IsCS*PartyLeft + IsCS*PartyRight
           + ref_type                 
           + Sex + Age + HasChildren + MasterDoktor + BornOutside + TimeinOffice, 
           weight=logTurnout
           ,
           data=subset(mydata), tol=1E-10,  x=T, y=T)
c47 <- robcov(f47, cluster=subset(mydata)$mp_id)
c47
DiscreteEffects(c47, medianspecial=c("IsCS", 0))
DiscreteEffects(c47, medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c47, "PartyLeft", "IsCS", medianspecial=c("IsCS", 1))
DiscreteEffectDiD(c47, "PartyRight", "IsCS", medianspecial=c("IsCS", 1))
contrast(c47, list(PartyLeft=1, IsCS=1), list(IsCS=1)) 
contrast(c47, list(PartyRight=1, IsCS=1), list(IsCS=1))   
 
 
#!3 Export
regressors <- c("IsCS",
                "PartyLeft", "PartyRight",
                "IsCS * PartyLeft",
                "IsCS * PartyRight",
                "IsCSFull * PartyLeft",
                "IsCSFull * PartyRight",
                "Personal characteristics"      
)

output <-
  mmp(              regressors=regressors,      #  Ohne regressors sollte vollst�ndige Liste
                    #  mit allen Variablen ausgegeben werden
                    file="clipboard",                                     # ... funktioniert NUR �ber clipboard
                    decimals=3,
                    factors = c("ref_type"),
                    
                    bottom.matter=c("r.squared", "Brier", "N"), # nicht notwendiges Argument
                    allmodels=list(dxPrint(c41), dxPrint(c42), dxPrint(c43), dxPrint(c44), dxPrint(c45), dxPrint(c46), dxPrint(c47), c41, c42, c43, c44, c45, c46, c47),
                    models.names=c("d41", "d42", "d43", "d44", "d45", "d46", "d47", "41", "42", "43", "44", "45", "46", "47"), # nicht notwendiges Argument
  )
write.table(output, file="out/appendix05.csv", sep=";")





  


